package leetcode.dynamic_programming;

/**
 * @author mgl
 * @version 1.0
 * @description TODO leecode 740 删除并获得点数
 * @date 2023/9/28 07:39
 */
public class DeleteAndEarn {

    //1.循环数组，找出最大值max，得出dp数组的大小为max+1(0-max)
    //2.统计数组元素，按照数值大小分类。比如数字1有5个，all[1]=5
    //3.最优子结构：dp[i] = max(dp[i-1],dp[i-2] * all[i])
    //解释：如果我删除当前元素，则说明在上一个元素中取到了最大值
    //如果我不删除当前元素，说明当前元素乘以个数 + 上两个元素的和取得了最大值
    public int deleteAndEarn(int[] nums) {
        if (nums.length == 1) return nums[0];

        int length = nums.length;
        int max = 0;
        for (int num : nums) {
            if (num > max) max = num;
        }
        int[] dp = new int[max + 1];
        int[] all = new int[max + 1];

        for (int num : nums) {
            all[num]++;
        }

        dp[0] = 0;
        dp[1] = all[1];
        for (int i = 2; i < max + 1; i++) {
            dp[i] = Math.max(dp[i - 2] + all[i] * i, dp[i - 1]);
        }

        return dp[max];
    }

    public static void main(String[] args) {
//        System.out.println(new DeleteAndEarn().deleteAndEarn(new int[]{2, 2, 3, 3, 3, 4}));
        System.out.println(new DeleteAndEarn().deleteAndEarn(new int[]{1,1}));
    }
}
